/*
** Mortem's HGE Addon 1.4
** Copyright (C) 2006-2007, Osokin 'elmortem' Makar
** http://elmortem.mirgames.ru
**
** HGE based GUI controls.
*/

#include <mhaguictrls.h>
#include <windows.h>
#include <stdio.h>

/*
** mhaGuiWindow (Picture)
*/

HGE *mhaGuiWindow::hge = 0;

mhaGuiWindow::mhaGuiWindow(int _id, int _shadow, float _x, float _y, float _tx, float _ty, float _tw, float _th, HTEXTURE _tex, bool _center) {
	hge = hgeCreate(HGE_VERSION);

	bStatic = true;
	bVisible = true;
	bEnabled = true;

	id = _id;
	shadow = _shadow;
	x = _x;
	y = _y;

	spr = new hgeSprite(_tex, _tx, _ty, _tw, _th);
	if(_center) spr->SetHotSpot(_tw / 2, _th / 2);
}

mhaGuiWindow::~mhaGuiWindow(void) {
	delete spr;

	hge->Release();
}

void mhaGuiWindow::Render(void) {
	if(shadow > 0) {
		spr->SetColor(0x44000000);
		spr->Render(x + shadow, y + shadow);
	}
	spr->SetColor(0xFFFFFFFF);
	spr->Render(x, y);
}

/*
** mhaGuiWindow2 (Picture from file)
*/

HGE *mhaGuiWindow2::hge = 0;

mhaGuiWindow2::mhaGuiWindow2(int _id, float _x, float _y, const HGE_CHAR *_filename, bool _center) {
	hge = hgeCreate(HGE_VERSION);

	bStatic = true;
	bVisible = true;
	bEnabled = true;

	id = _id;
	x = _x;
	y = _y;

	if(!_filename || !*_filename) {
		bVisible = false;
		bEnabled = false;
		tex = 0;
		spr = 0;
		return;
	}
	tex = hge->Texture_Load(_filename);
	if(tex) {
		spr = new hgeSprite(tex, 0.f, 0.f, (float)hge->Texture_GetWidth(tex), (float)hge->Texture_GetHeight(tex));
		if(_center) spr->SetHotSpot(hge->Texture_GetWidth(tex) * 0.5f, (float)hge->Texture_GetHeight(tex) * 0.5f);
	} else {
		bVisible = false;
		bEnabled = false;
		spr = 0;
	}
}

mhaGuiWindow2::~mhaGuiWindow2(void) {
	if(tex) {
		hge->Texture_Free(tex);
		delete spr;
	}

	hge->Release();
}

void mhaGuiWindow2::Render(void) {
	if(tex) {
		spr->SetColor(0x44000000);
		spr->Render(x + 5, y + 5);
		spr->SetColor(0xFFFFFFFF);
		spr->Render(x, y);
	}
}

/*
** mhaGuiCheck
*/

mhaGuiCheck::mhaGuiCheck(int _id, hgeFont *_font, float _fntscale, int _fntcolor, float _x, float _y, float _tx, float _ty, float _tw, float _th, HTEXTURE  _tex, const HGE_CHAR *_text, mhaCallback *callback, bool _check) {
	id = _id;

	font = _font;
	fntScale = _fntscale;
	fntColor = _fntcolor;

  Callback = callback;

	x = _x;
	y = _y;

	bStatic = false;
	bVisible = true;
	bEnabled = true;

	sprNoCheck = new hgeSprite(_tex, _tx, _ty, _tw, _th);
	sprNoCheck->SetHotSpot(_tw / 2, _th / 2);
	sprCheck = new hgeSprite(_tex, _tx + _tw, _ty, _tw, _th);
	sprCheck->SetHotSpot(_tw / 2, _th / 2);

	rect.Set(_x - _tw / 2, _y - _th / 2, _x + _tw / 2, _y + _th / 2);

	if(_text && *_text) {
		text = new HGE_CHAR[HGE_SLEN(_text) + 1];
		HGE_SCPY(text, _text);
	} else
		text = NULL;

	font->SetScale(fntScale);
	tx = x + _tw;
	ty = y - font->GetHeight() * font->GetScale() / 2;

	isCheck = _check;
}

mhaGuiCheck::~mhaGuiCheck(void) {
	if(text && *text) delete text;
}

void mhaGuiCheck::Render(void) {
	if(isCheck) {
		sprCheck->Render(x, y);
	} else {
		sprNoCheck->Render(x, y);
	}
	font->SetScale(fntScale);
	font->SetColor(fntColor);
	font->Render(tx, ty, HGETEXT_LEFT, text);
}

bool mhaGuiCheck::MouseLButton(bool bDown) {
	if(bDown) {
    if(Callback) Callback->call(MHACB_GUI, MHAGUI_CLICK);
    isCheck = !isCheck;
  }
	return false;
}

bool mhaGuiCheck::GetCheck(void) {
	return isCheck;
}

void mhaGuiCheck::SetCheck(bool _check) {
	isCheck = _check;
}

/*
** mhaGuiButton
*/

mhaGuiButton::mhaGuiButton(int _id, float _x, float _y, float _tx, float _ty, float _tw, float _th, HTEXTURE  _tex) {
	id = _id;

	x = _x - _tw / 2;
	y = _y - _th / 2;

	bStatic = false;
	bVisible = true;
	bEnabled = true;

	sprNormal = new hgeSprite(_tex, _tx, _ty, _tw, _th);
	sprOver = new hgeSprite(_tex, _tx + _tw, _ty, _tw, _th);

	rect.Set(_x - _tw / 2, _y - _th / 2, _x + _tw / 2, _y + _th / 2);

	isOver = false;
}

mhaGuiButton::~mhaGuiButton(void) {
	delete sprNormal;
	delete sprOver;
}

void mhaGuiButton::Render(void) {
	if(isOver)
		sprOver->Render(x, y);
	else
		sprNormal->Render(x, y);
}

void mhaGuiButton::MouseOver(bool bOver) {
	isOver = bOver;
}

bool mhaGuiButton::MouseLButton(bool bDown) {
	if(bDown) {
		isOver = false;
		return false;
	} else {
		isOver = true;
		return true;
	}
}

/*
** mhaGuiTextButton
*/

mhaGuiTextButton::mhaGuiTextButton(int _id, hgeFont *_font, int _shadow, float _fntscale, int _fntcolornormal, int _fntcolorover, float _x, float _y, float _tx, float _ty, float _tw, float _th, float _allwidth, int _align, HTEXTURE  _tex, const HGE_CHAR *_text, mhaCallback *callback) {
	id = _id;
	font = _font;
	shadow = _shadow;
	fntScale = _fntscale;
	fntColorNormal = _fntcolornormal;
	fntColorOver = _fntcolorover;

	bStatic = false;
	bVisible = true;
	bEnabled = true;
	
	Callback = callback;

	if(_tex != 0) {
		isSprite = true;

		sprNormalLeft = new hgeSprite(_tex, _tx, _ty, _tw, _th);
		sprNormalCenter = new hgeSprite(_tex, _tx + _tw, _ty, _tw, _th);
		sprNormalRight = new hgeSprite(_tex, _tx + _tw * 2, _ty, _tw, _th);
	
		sprOverLeft = new hgeSprite(_tex, _tx + _tw * 3, _ty, _tw, _th);
		sprOverCenter = new hgeSprite(_tex, _tx + _tw * 4, _ty, _tw, _th);
		sprOverRight = new hgeSprite(_tex, _tx + _tw * 5, _ty, _tw, _th);
		//sprOverLeft->SetBlendMode(BLEND_COLORADD | BLEND_ALPHAADD | BLEND_NOZWRITE);
		//sprOverCenter->SetBlendMode(BLEND_COLORADD | BLEND_ALPHAADD | BLEND_NOZWRITE);
		//sprOverRight->SetBlendMode(BLEND_COLORADD | BLEND_ALPHAADD | BLEND_NOZWRITE);
	} else {
    _th = font->GetHeight() * font->GetScale();
		isSprite = false;
	}

	if(_text && *_text) {
		text = new HGE_CHAR[HGE_SLEN(_text) + 1];
		HGE_SCPY(text, _text);
	} else
		text = 0;

	font->SetScale(fntScale);
	if(_allwidth == 0)
		tw = font->GetStringWidth(text) - _tw;
	else
		tw = _allwidth - _tw * 2;

	align = _align;
	if(align == BUTTON_CENTER) { // center
		x = _x - tw / 2 - _tw;
	} else if(align == BUTTON_LEFT) { // left
		x = _x;
	} else if(align == BUTTON_RIGHT) { // right
		x = _x - tw - _tw * 2;
	}
	tx = x + tw / 2 + _tw;
	y = _y - _th / 2;
	w = _tw;
	h = _th;

	rect.Set(x, y, x + tw + _tw * 2, y + _th);

	isOver = false;
}

mhaGuiTextButton::~mhaGuiTextButton(void) {
	if(isSprite) {
		delete sprNormalLeft;
		delete sprNormalCenter;
		delete sprNormalRight;
		delete sprOverLeft;
		delete sprOverCenter;
		delete sprOverRight;
	}
	if(text && *text) delete text;
}

void mhaGuiTextButton::SetText(const HGE_CHAR *_text) {
	if(text && *text) delete text;
	if(_text && *_text) {
		text = new HGE_CHAR[HGE_SLEN(_text) + 1];
		HGE_SCPY(text, _text);
	} else
		text = 0;
}

HGE_CHAR *mhaGuiTextButton::GetText(void) {
	return text;
}

void mhaGuiTextButton::Render(void) {
	int _color = fntColorNormal;
	float offset = 0;
	
	font->SetScale(fntScale);

	if(isOver) {
		if(shadow != 0) offset = (shadow * 0.5f) * fntScale;
		_color = fntColorOver;
		if(isSprite) {
			sprOverLeft->Render(x, y);
			sprOverCenter->RenderStretch(x + w, y, x + w + tw, y + h);
			sprOverRight->Render(rect.x1 + w + tw, y);
		}
	} else {
		if(isSprite) {
			sprNormalLeft->Render(x, y);
			sprNormalCenter->RenderStretch(x + w, y, x + w + tw, y + h);
			sprNormalRight->Render(x + w + tw, y);
		}
	}

	float _y = y + h / 2 - font->GetHeight() * font->GetScale() / 2;
	if(shadow != 0) {
		font->SetColor(0x66000000);
		font->Render(tx + shadow + offset, _y + shadow + offset, HGETEXT_CENTER, text);
	}
	font->SetColor(_color);
	font->Render(tx - offset, _y - offset, HGETEXT_CENTER, text);
	
	/*if(isOver) {
		if(isSprite) {
			sprOverLeft->Render(x, y);
			sprOverCenter->RenderStretch(x + w, y, x + w + tw, y + h);
			sprOverRight->Render(x + w + tw, y);
		}
	}*/
}

void mhaGuiTextButton::Update(float fDeltaTime) {
	if(x != rect.x1 || y != rect.y1) {
		tx += rect.x1 - x;
		x = rect.x1;
		y = rect.y1;
	}
}

void mhaGuiTextButton::MouseOver(bool bOver) {
  if(!isOver && Callback) Callback->call(MHACB_GUI, MHAGUI_OVER);
	isOver = bOver;
}

bool mhaGuiTextButton::MouseLButton(bool bDown) {
	if(bDown) {
    if(Callback) Callback->call(MHACB_GUI, MHAGUI_CLICK);
		isOver = false;
		return false;
	} else {
		isOver = true;
		return true;
	}
}

/*
** mhaGuiListbox
*/

mhaGuiListbox::mhaGuiListbox(int _id, float x, float y, float w, float h, hgeFont *fnt, float _fntscale, DWORD tColor, DWORD thColor, DWORD hColor, bool _selected)
{
	id=_id;
	bStatic=false;
	bVisible=true;
	bEnabled=true;
	bSelected = _selected;
	rect.Set(x, y, x+w, y+h);
	font=fnt;
	fntScale = _fntscale;
	font->SetScale(fntScale);
	sprHighlight=new hgeSprite(0, 0, 0, w, fnt->GetHeight() * font->GetScale());
	sprHighlight->SetColor(hColor);
	textColor=tColor;
	texthilColor=thColor;
	pItems=0;
	nItems=0;

	nSelectedItem=-1;
	nTopItem=0;
	mx=0; my=0;
}

mhaGuiListbox::~mhaGuiListbox()
{
	Clear();
	if(sprHighlight) delete sprHighlight;
}


int mhaGuiListbox::AddItem(HGE_CHAR *item, void *userdata)
{
	mhaGuiListboxItem *pItem=pItems, *pLast=0, *pNew;

	pNew = new mhaGuiListboxItem();
	pNew->text = new HGE_CHAR[HGE_SLEN(item)+1];
	HGE_SCPY(pNew->text, item);
	//pNew->text[wcslen(pNew->text)-1]='\0';
	pNew->userdata = userdata;
	pNew->next = 0;

	while(pItem) { pLast=pItem;	pItem=pItem->next; }

	if(pLast) pLast->next = pNew;
	else pItems = pNew;
	nItems++;

	return nItems-1;
}

void mhaGuiListbox::DeleteItem(int n)
{
	mhaGuiListboxItem *pItem=pItems, *pPrev=0, *pFind = 0;

	if(n<0 || n>=GetNumItems()) return;

	int c = 0;
	while(pItem) {
		if(c == n) {
			pFind = pItem;
			break;
		}
		pPrev=pItem;
		pItem=pItem->next;
		c++;		
	}

	if(pPrev) pPrev->next=pFind->next;
	else pItems=pFind->next;
	delete[] pFind->text;
	delete pFind;
	nItems--;
}

HGE_CHAR *mhaGuiListbox::GetItemText(int n)
{
	int i;
	mhaGuiListboxItem *pItem=pItems;

	if(n<0 || n>=GetNumItems() || !bSelected) return 0;

	for(i=0;i<n;i++) pItem=pItem->next;

	return pItem->text;
}

void mhaGuiListbox::Clear()
{
	mhaGuiListboxItem *pItem=pItems, *pNext = 0;

	while(pItem)
	{
		pNext=pItem->next;
		delete[] pItem->text;
		delete pItem;
		pItem=pNext;
	}
	
	nItems=0;
	pItems = NULL;
}

void mhaGuiListbox::SetSelectedItem(int n) {
	if(/*n>=0 && */n<GetNumItems()) {
		nSelectedItem=n;
	}
	if(nSelectedItem > nTopItem+GetNumRows() - 1) nTopItem = nSelectedItem - GetNumRows() + 1;
	if(nSelectedItem < 0) nTopItem = nItems - GetNumRows() + 1;
	if(nTopItem < 0) nTopItem = 0;
}

void mhaGuiListbox::Render() {
	int i;
	mhaGuiListboxItem *pItem=pItems;

	font->SetScale(fntScale);
	for(i=0;i<nTopItem;i++) pItem=pItem->next;

	int cnt = GetNumRows();
	if(cnt > nItems - nTopItem) cnt = nItems - nTopItem;

	for(i = 0; i < cnt; i++) {
		//font->SetColor(0x30000000);
		//font->Render(rect.x1 + 5, rect.y1 + i * font->GetHeightScale() + 2, HGETEXT_LEFT, pItem->text);
		if(nTopItem + i == nSelectedItem && bSelected) {
			sprHighlight->Render(rect.x1, rect.y1 + i * font->GetHeight() * font->GetScale());
			font->SetColor(texthilColor);
		} else font->SetColor(textColor);
		font->Render(rect.x1 + 3, rect.y1 + i * font->GetHeight() * font->GetScale(), HGETEXT_LEFT, pItem->text);
		if(i >= nItems - nTopItem) return;
		if(pItems->next == 0) return;
		pItem = pItem->next;
	}
}

bool mhaGuiListbox::MouseLButton(bool bDown)
{
	int nItem;

	if(bDown && bSelected) {
		font->SetScale(fntScale);
		nItem = nTopItem + int(my) / int(font->GetHeight() * font->GetScale());
		if(nItem < nItems) {
			nSelectedItem = nItem;
			return true;
		}
	}
	return false;
}


bool mhaGuiListbox::MouseWheel(int nNotches)
{
	nTopItem -= nNotches;
	if(nTopItem > GetNumItems() - GetNumRows()) nTopItem = GetNumItems() - GetNumRows();
	if(nTopItem < 0) nTopItem = 0;

	return true;
}

bool mhaGuiListbox::KeyClick(int key, int chr)
{
	if(!bSelected) return false;
	switch(key)
	{
		case HGEK_DOWN:
			if(nSelectedItem < nItems-1)
			{
				nSelectedItem++;
				if(nSelectedItem > nTopItem+GetNumRows()-1) nTopItem=nSelectedItem-GetNumRows()+1;
				return true;
			}
			break;

		case HGEK_UP:
			if(nSelectedItem > 0)
			{
				nSelectedItem--;
				if(nSelectedItem < nTopItem) nTopItem=nSelectedItem;
				return true;
			}
			break;
	}
	return false;
}

/*
** mhaGuiInput
*/

mhaGuiInput::mhaGuiInput(int _id, hgeFont* _fnt, float _fntscale, int _shadow, float _x, float _y, float _width, int _color, int _max, const HGE_CHAR *prevalue){ 
	id = _id;
	font = _fnt; 
	fntScale = _fntscale;
	shadow = _shadow;
	width = _width;
	x = _x; 
	y = _y; 
	color = _color;
	max = _max;
	bStatic = false; 
	bVisible = true; 
	bEnabled = true; 
	caretVisible = true; 
	focused = false; 
	caretTimer = 0.0f; 
	caret = TXT("_"); 
	content = new HGE_CHAR[max + 1];
	if(prevalue && *prevalue) {
		HGE_SNCPY(content, prevalue, max); 
		int prevalue_length = (int)HGE_SLEN(prevalue); 
		if(prevalue_length < max) { 
			caretposition = prevalue_length; 
		} else { 
			caretposition = max - 1; 
		}
	} else {
		caretposition = 0;
	}
	content[caretposition] = TXT('\0'); 
	content[max] = TXT('\0'); //Last wchar_t will always be a string terminator 
	font->SetScale(fntScale);
	rect.Set(_x, _y, _x + _width, _y + font->GetHeight() * font->GetScale()); 
}

mhaGuiInput::~mhaGuiInput() { 
	/*if(content && *content) */delete content;
} 

void mhaGuiInput::Render() { 
	font->SetScale(fntScale);
	if(shadow) {
		font->SetColor(0xFF000000);
		font->Render(rect.x1 + (rect.x2-rect.x1) * 0.5f + shadow, rect.y1 + shadow, HGETEXT_CENTER, content); 
		//Renders the caret 
		if(focused && caretVisible) { 
			float w = font->GetStringWidth(content) * 0.5f; 
			font->Render(rect.x1 + (rect.x2-rect.x1) * 0.5f + w + shadow, rect.y1 + shadow, HGETEXT_LEFT, caret); 
		}
	}
	font->SetColor(color);
  font->Render(rect.x1 + (rect.x2-rect.x1) * 0.5f, rect.y1, HGETEXT_CENTER, content); 
  //Renders the caret 
  if(focused && caretVisible) { 
		float w = font->GetStringWidth(content) * 0.5f; 
    font->Render(rect.x1 + (rect.x2-rect.x1) * 0.5f + w, rect.y1, HGETEXT_LEFT, caret); 
  }

	//hge->Gfx_RenderLine(rect.x1, rect.y1, rect.x2, rect.y1, 0xFFFF0000);
	//hge->Gfx_RenderLine(rect.x1, rect.y2, rect.x2, rect.y2, 0xFFFFFF00);
} 

//Gets Focus with LeftMouseKlick 
bool mhaGuiInput::MouseLButton(bool bDown){ 
	if(bDown) { 
		return false; 
	} 
	gui->SetFocus(id); 
	return false; 
}

//Saving focus state in focused 
void mhaGuiInput::Focus(bool bFocused){ 
	if(!bFocused) { 
		focused = false; 
	} else { 
		focused = true; 
	}
}

void mhaGuiInput::Update(float fDeltaTime){ 
    caretTimer += fDeltaTime; 
   /*Makes the caret blinking effect 
     You might want to change the value below for slower/faster blinking*/ 
   if(caretTimer >= 0.2f) {
      caretVisible = !caretVisible;
      caretTimer = 0.0f;
   } 
} 

bool mhaGuiInput::KeyClick(int key, int chr){ 
	//Loses Focus with Enter 
	if(key == HGEK_ENTER){ 
		//gui->SetFocus(-1); 
		return true; 
	} 
	if(key == HGEK_TAB) {
		gui->SetFocus(id + 1);
		return true;
	}
   HGE_CHAR *tmp = new HGE_CHAR[max + 1];
   HGE_SCPY(tmp, content);
	 char c[2];
	 HGE_CHAR cc[2];

   if (key == HGEK_BACKSPACE){ //Delete one wchar_tacter per backspace 
      if (caretposition != 0){ 
         caretposition -=1; 
         tmp[caretposition] = '\0'; //Put string terminator one wchar_tacter back 
      } 
      HGE_SNCPY(content, tmp, max);
			delete tmp;
      return false; 
   } 
   /*When chr==0 no ascii-key (e.g. shift) is pressed and chr would 
     be saved as string terminator. The other thing to look after, is 
     whether we have max. wchar_tacters*/ 
   if (chr != 0 && caretposition < max - 1) { 
		 c[0] = chr;
		 c[1] = 0;
		 C2W(c, cc, 2);
		 HGE_SPRINTF(tmp, TXT("%s%s"), content, cc);
		 hge->System_Log(cc);
/*#ifdef UNICODE
		 swprintf(tmp, L"%s%S", content, c);
#else
		 sprintf(tmp, "%s%s", content, c);
#endif*/
      //tmp[caretposition] = chr; 
      tmp[caretposition+1] = '\0'; //add overwritten string terminator 
      //Assures that the String will not take to much place 
      if (font->GetStringWidth(tmp) <= width){ 
         HGE_SNCPY(content, tmp, max); //Store back 
         caretposition+=1; 
      } 
   }
	 delete tmp;
   return false; 
}

//Setting the Text 
void mhaGuiInput::SetText(const HGE_CHAR *_text){ 
	if(_text && *_text) {
		HGE_SNCPY(content, _text, max);
		caretposition = HGE_SLEN(content);
	} else {
		content[0] = '\0';
		caretposition = 0;
	}
}

HGE_CHAR *mhaGuiInput::GetText(){ 
   return content; 
}

void mhaGuiInput::SetPos(float _x, float _y) {
	x = _x;
	y = _y;
	font->SetScale(fntScale);
	rect.Set(x, y, x + width, y + font->GetHeight() * font->GetScale()); 
}

/*
** mhaGuiScroll
*/

mhaGuiScroll::mhaGuiScroll(int _id, float _x, float _y, float _size, float _tx, float _ty, float _tw, float _th, HTEXTURE  _tex, bool _vertical) {
	id = _id;
	bStatic = false;
	bVisible = true;
	bEnabled = true;
	isPressed = false;
	isVertical = _vertical;

	x = _x;
	y = _y;
	tw = _tw;
	th = _th;
	pos = 0;

	sprBegin = new hgeSprite(_tex, _tx, _ty, _tw, _th);
	if(_vertical) {
		sprCenter = new hgeSprite(_tex, _tx, _ty + _th, _tw, _th);
		sprEnd = new hgeSprite(_tex, _tx, _ty + _th * 2, _tw, _th);
		sprButton = new hgeSprite(_tex, _tx, _ty + _th * 3, _tw, _th);
		sprButton->SetHotSpot(0, _th / 2);
		rect.Set(_x, _y, _x + _tw, _y + _size);
		size = _size - _th * 2;
	} else {
		sprCenter = new hgeSprite(_tex, _tx + _tw, _ty, _tw, _th);
		sprEnd = new hgeSprite(_tex, _tx + _tw * 2, _ty, _tw, _th);
		sprButton = new hgeSprite(_tex, _tx + _tw * 3, _ty, _tw, _th);
		sprButton->SetHotSpot(_tw / 2, 0);
		rect.Set(_x, _y, _x + _size, _y + _th);
		size = _size - _tw * 2;
	}
}

mhaGuiScroll::~mhaGuiScroll(void) {
	delete sprBegin;
	delete sprCenter;
	delete sprEnd;
	delete sprButton;
}

void mhaGuiScroll::Render(void) {
	if(max - min <= 0) return;

	/*if(isPressed) {
		sprBegin->SetColor(0xFFFFDFDF);
		sprCenter->SetColor(0xFFFFDFDF);
		sprEnd->SetColor(0xFFFFDFDF);
	} else {
		sprBegin->SetColor(0xFFFFFFFF);
		sprCenter->SetColor(0xFFFFFFFF);
		sprEnd->SetColor(0xFFFFFFFF);
	}*/
	sprBegin->Render(x, y);
	if(isVertical) {
		sprCenter->RenderStretch(x, y + th, x + tw, y + th + size);
		sprEnd->Render(x, y + th + size);
		sprButton->Render(x, y + /*th / 2 + (size + th) / (max - min) * (value - min)*/ + pos);
	} else {
		sprCenter->RenderStretch(x + tw, y, x + tw + size, y + th);
		sprEnd->Render(x + tw + size, y);
		sprButton->Render(x + /*tw / 2 + (size + tw) / (max - min) * (value - min)*/ + pos, y);
	}
}

bool mhaGuiScroll::MouseLButton(bool bDown) {
	isPressed = bDown;
	return false;
}

bool mhaGuiScroll::MouseMove(float _x, float _y) {
	if(isPressed) {
		if(isVertical) {
			if(_y > rect.y2 - rect.y1) _y = rect.y2 - rect.y1;
			if(_y < 0) _y = 0;
			value = min + (max - min) * _y / (rect.y2 - rect.y1);
			
      pos = _y;
			if(_y < th / 2) pos = th / 2;
			if(_y > rect.y2 - rect.y1 - th / 2) pos = rect.y2 - rect.y1 - th / 2;
		} else {
			if(_x > rect.x2 - rect.x1) _x = rect.x2 - rect.x1;
			if(_x < 0) _x = 0;
			value = min + (max - min) * _x / (rect.x2 - rect.x1);
			
			pos = _x;
			if(_x < tw / 2) pos = tw / 2;
			if(_x > rect.x2 - rect.x1 - tw / 2) pos = rect.x2 - rect.x1 - tw / 2;
		}
		return true;
	}
	return false;
}

void mhaGuiScroll::SetMode(float _min, float _max) {
	min = _min;
	max = _max;
}

void mhaGuiScroll::SetValue(float _value) {
	value = _value;
	if(value > max) value = max;
	if(value < min) value = min;
	
	if(isVertical) {
    pos = th / 2 + (size + th) / (max - min) * (value - min);
  } else {
  	pos = tw / 2 + (size + tw) / (max - min) * (value - min);
  }
}

float mhaGuiScroll::GetValue(void) {
	return value;
}

/*
** mhaGuiText
*/
mhaGuiText::mhaGuiText(int _id, hgeFont *_font, float _size, int _color, int _shadow, float _x, float _y, float _width, float _height, int _align, const HGE_CHAR *_text) {
	id = _id;
	bStatic = true;
	bVisible = true;
	bEnabled = true;
	
	font = _font;
	fontSize = _size;
	fontColor = _color;
	fontAlign = _align;
	shadow = _shadow;
	x = _x;
	y = _y;
	width = _width;
	height = _height;
	
	text = 0;
	SetText(_text);

	if(fontAlign == HGETEXT_CENTER) {
		x -= width * 0.5f;
	}
}

mhaGuiText::~mhaGuiText(void) {
	if(text && *text) {
		delete text;
	}
}

void mhaGuiText::Render(void) {
	//hge->System_Log("mhaGuiText.Render(%s)", text);
	if(text && *text) {
		//hge->System_Log("%s", text);
		font->SetScale(fontSize);
		if(shadow) {
			font->SetColor(0x99000000);
			font->printfb(x + shadow, y + shadow, width, height, fontAlign, text);
		}
		font->SetColor(fontColor);
		font->printfb(x, y, width, height, fontAlign, text);
	}
}

void mhaGuiText::SetText(const HGE_CHAR *_text) {
	if(text && *text) {
		delete[] text;
	}
	text = 0;
	if(_text && *_text) {
		text = new HGE_CHAR[HGE_SLEN(_text) + 1];
		HGE_SCPY(text, _text);
	}
}
